<?php

namespace App\Model\Guild;

use App\Model\AdminUsers;
use App\Model\UserProfile;

/**
 * 代理
 */
class Guild extends GuildBase
{
    protected $table = 'guild_manage';
    protected $fillable = ['status'];

    public function setCreditPicsAttribute($images)
    {
        if (is_array($images)) {
            $this->attributes['credit_pics'] = json_encode($images);
            // $this->attributes['id_card_pics'] = json_encode($images);
        }
    }

    public function setIdCardPicsAttribute($images)
    {
        if (is_array($images)) {
            $this->attributes['id_card_pics'] = json_encode($images);
        }
    }

    public function getIdCardPicsAttribute($images)
    {
        return json_decode($images, true);
    }

    public function getCreditPicsAttribute($images)
    {
        return json_decode($images, true);
    }

    /**
     * 生成代理ID
     */
    static function buildGuildId($id)
    {
        return 600000 + $id;
    }

    /**
     * 生成代理ID
     */
    static function buildPwd($name = '')
    {
        return substr(md5($name), 0, 8);
    }

    /**
     * 更新账号
     */
    static function updateAccount($id = 0, $guildId = '', $loginAccount = '', $loginPwd = '')
    {
        return self::where('id', $id)->limit(1)->update([
            'login_account' => $loginAccount,
            'login_pwd' => $loginPwd,
            'guild_id' => $guildId
        ]);
    }

    /**
     * 根据登录ID获取代理ID
     */
    static function getIdByLoginId($loginId = '')
    {
        $item = self::where('guild_id', $loginId)->first();
        return $item ? $item->id : 0;
    }

    static function getAdminids()
    {
        $adminIds = self::where([['admin_id', '<>', 0]])->pluck('admin_id')->toArray();
        return AdminUsers::whereIn('id', $adminIds)->pluck('name', 'id')->toArray();
    }

    static function getInfo()
    {
        return self::pluck('name', 'id')->toArray();
    }

    static function getUserId($userId){
        return self::where('user_id',$userId)->first();
    }
}
